<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace cmf\controller;

use app\lib\User\UserHandleLog\AddUserHandleLog;
use app\lib\User\UserHandleLog\CompletionUserHandleLog;
use think\Db;

class AdminBaseController extends BaseController
{
    protected $loginId = 0;
    protected $publicStatus = [];
    public function _initialize()
    {
        // 监听admin_init
        hook('admin_init');
        parent::_initialize();
        $session_admin_id = session('ADMIN_ID');
        if (!empty($session_admin_id)) {
            $user = Db::name('user')->where(['id' => $session_admin_id])->find();

            if (!$this->checkAccess($session_admin_id)) {
                $this->error("您没有访问权限！");
            }
            $this->assign("admin", $user);
        } else {
            if ($this->request->isPost()) {
                $this->error("您还没有登录！", url("admin/public/login"));
            } else {
                header("Location:" . url("admin/public/login"));
                exit();
            }
        }
        $this->loginId =  session('ADMIN_ID');
        $this->saveUserHandleLog();
        /*引入通用配置*/
        $this->includeConfig();
    }

    public function _initializeView()
    {
        $cmfAdminThemePath    = config('cmf_admin_theme_path');
        $cmfAdminDefaultTheme = cmf_get_current_admin_theme();

        $themePath = "{$cmfAdminThemePath}{$cmfAdminDefaultTheme}";

        $root = cmf_get_root();

        //使cdn设置生效
        $cdnSettings = cmf_get_option('cdn_settings');
        if (empty($cdnSettings['cdn_static_root'])) {
            $viewReplaceStr = [
                '__ROOT__'     => $root,
                '__TMPL__'     => "{$root}/{$themePath}",
                '__STATIC__'   => "{$root}/static",
                '__WEB_ROOT__' => $root
            ];
        } else {
            $cdnStaticRoot  = rtrim($cdnSettings['cdn_static_root'], '/');
            $viewReplaceStr = [
                '__ROOT__'     => $root,
                '__TMPL__'     => "{$cdnStaticRoot}/{$themePath}",
                '__STATIC__'   => "{$cdnStaticRoot}/static",
                '__WEB_ROOT__' => $cdnStaticRoot
            ];
        }

        $viewReplaceStr = array_merge(config('view_replace_str'), $viewReplaceStr);
        config('template.view_base', "$themePath/");
        config('view_replace_str', $viewReplaceStr);
    }

    /**
     * 初始化后台菜单
     */
    public function initMenu()
    {
    }

    /**
     *  检查后台用户访问权限
     * @param int $userId 后台用户id
     * @return boolean 检查通过返回true
     */
    private function checkAccess($userId)
    {
        // 如果用户id是1，则无需判断
        if ($userId == 1) {
            return true;
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();
        $rule       = $module . $controller . $action;

        $notRequire = ["adminIndexindex", "adminMainindex"];
        if (!in_array($rule, $notRequire)) {
            return cmf_auth_check($userId);
        } else {
            return true;
        }
    }

    /**
     * 记录用户的操作日志
     */
    protected function saveUserHandleLog(){
        $controller = $this->request->controller();
        $action = $this->request->action();
        $ip = $this->request->ip();
        $uid = session('ADMIN_ID');
        $userLog = new AddUserHandleLog();
        $logId =  $userLog->setOpUid($uid)->setOpAction($action)
            ->setOpController($controller)->setIp($ip)->save();
        session('opLog',$logId);
    }

    /**
     * 对于赠删改的数据进行操作日志补全监控
     * @param $data 请求参数
     * @param $result 请求后的结果
     * @param $type     操作类型
     * @param $resultId 被操作的id
     */
    protected function updateUserHandleLog($data='',$result='',$type='access',$resultId='0'){
        (new CompletionUserHandleLog(session('opLog')))
            ->setDataResult($result)->setRequestData($data)
            ->setType($type)->setResultId($resultId)->save();
    }


    protected function checkPost($data,$array = []){
        foreach ($array as $k=>$v){
            if(!isset($data[$k])){
                $data[$k] = $v;
            }
        }
        return $data;
    }

    private function includeConfig(){
        if (!file_exists( CMF_ROOT . "data/conf/publicStatus.php")) {
            file_put_contents(CMF_ROOT . "data/conf/publicStatus.php",'');
        }
        $this->publicStatus = include CMF_ROOT . "data/conf/publicStatus.php";
    }
}